package com.itbaizhan;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class ResponseByteServletTwo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //给定图片的路径
        File file = new File("e:/picture/1002.jpg");
        //创建读取图片的IO流
        InputStream is = new FileInputStream(file);
        //创建缓冲区
        byte[] buff = new byte[is.available()];
        //将字节读进缓冲区
        is.read(buff);
        //设置响应类型
        resp.setContentType("image/jpeg");
        //输出流
        OutputStream os = resp.getOutputStream();
        //将字节写出
        os.write(buff);
        os.flush();
        os.close();

    }
}
